home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day04 / airplane.cpp next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  2.8 KB  |  138 lines

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #pragma hdrstop
  4.  
  5. #include "airplane.h"
  6. //
  7. // Constructor performs initialization
  8. //
  9. Airplane::Airplane(const char* _name, int _type) :
  10.   type(_type),
  11.   status(ONRAMP),
  12.   speed(0),
  13.   altitude(0),
  14.   heading(0)
  15. {
  16.   switch (type) {
  17.     case AIRLINER : ceiling = 35000; break;
  18.     case COMMUTER : ceiling = 20000; break;
  19.     case PRIVATE  : ceiling = 8000;
  20.   }
  21.   name = new char[50];
  22.   strcpy(name, _name);
  23. }
  24. //
  25. // Destructor performs cleanup.
  26. //
  27. Airplane::~Airplane()
  28. {
  29.   delete[] name;
  30. }
  31. //
  32. // Gets a message from the user.
  33. //
  34. bool
  35. Airplane::SendMessage(int msg, char* response,
  36.   int spd, int dir, int alt)
  37. {
  38.   //
  39.   // Check for bad commands.
  40.   //
  41.   if (spd > 500) {
  42.     strcpy(response, "Speed cannot be more than 500.");
  43.     return false;
  44.   }
  45.   if (dir > 360) {
  46.     strcpy(response, "Heading cannot be over 360 degrees.");
  47.     return false;
  48.   }
  49.   if (alt < 100 && alt != -1) {
  50.     strcpy(response, "I'd crash, bonehead!");
  51.     return false;
  52.   }
  53.   if (alt > ceiling) {
  54.     strcpy(response, "I can't go that high.");
  55.     return false;
  56.   }
  57.   //
  58.   // Do something base on which command was sent.
  59.   //
  60.   switch (msg) {
  61.     case MSG_TAKEOFF : {
  62.       // Can't take off if already in the air!
  63.       if (status != ONRAMP) {
  64.         strcpy(response, "I'm already in the air!");
  65.         return false;
  66.       }
  67.       TakeOff(dir);
  68.       break;
  69.     }
  70.     case MSG_CHANGE : {
  71.       // Can't change anything if on the ground.
  72.       if (status == ONRAMP) {
  73.         strcpy(response, "I'm on the ground");
  74.         return false;
  75.       }
  76.        // Only change if a non-negative value was passed.
  77.       if (spd != -1) speed = spd;
  78.       if (dir != -1) heading = dir;
  79.       if (alt != -1) altitude = alt;
  80.       status == CRUISING;
  81.       break;
  82.     }
  83.     case MSG_LAND : {
  84.       if (status == ONRAMP) {
  85.         strcpy(response, "I'm already on the ground.");
  86.         return false;
  87.       }
  88.       Land();
  89.       break;
  90.     }
  91.     case MSG_REPORT : ReportStatus();
  92.   }
  93.   //
  94.   // Standard reponse if all went well.
  95.   //
  96.   strcpy(response, "Roger.");
  97.   return true;
  98. }
  99. //
  100. // Perform takeoff.
  101. //
  102. void
  103. Airplane::TakeOff(int dir)
  104. {
  105.   heading = dir;
  106.   status = TAKINGOFF;
  107. }
  108. //
  109. // Perform landing.
  110. //
  111. void
  112. Airplane::Land()
  113. {
  114.   speed = heading = altitude = 0;
  115.   status == ONRAMP;
  116. }
  117. //
  118. // Build a string to report the airplane's status.
  119. //
  120. int
  121. Airplane::GetStatus(char* statusString)
  122. {
  123.   sprintf(statusString, "%s, Altitude: %d, Heading: %d, "
  124.     "Speed: %d\n", name, altitude, heading, speed);
  125.   return status;
  126. }
  127. //
  128. // Get the status string and output it to the screen.
  129. //
  130. void
  131. Airplane::ReportStatus()
  132. {
  133.   char buff[100];
  134.   GetStatus(buff);
  135.   cout << endl << buff << endl;
  136. }
  137.  
  138.